﻿<%@ WebHandler Language="C#" Class="UploadFile" %>

using System;
using System.Web;
using System.IO;

public class UploadFile : AjaxHandlerBase
{

    //*******************************************
    //功能概要       ：重写的请求处理方法
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：父类中隐藏了IHttpHandler接口方法，本类中须对本方法进行重写
    //作成者         ：姚志强
    //作成日         ：2010-03-25
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        //处理类型
        string strDealType = context.Request.Params["DealType"];

        switch (strDealType)
        {
            case "CutterImage":
                SaveCutterImage(context);
                break;
            case "ThumbnialImage":
                SaveThumbnialImage(context);
                break;
            case "WaterMarkImage":
                SaveWaterMarkImage(context);
                break;
            default :
                break;
        }

        if (context.Request["op"] != null)
        {
            switch (context.Request["op"].ToLower())
            {
                case "fileupload":
                    FileUpload(context);
                    break;
            }
        }
    }

    /// <summary>
    /// 普通文件文件
    /// </summary>
    /// <param name="context"></param>
    private void FileUpload(HttpContext context)
    {
        try
        {
            HttpPostedFile file = context.Request.Files["FileData"];
            string localPath = "~/UploadFile/File/";

            string savePath = context.Server.MapPath(localPath);
            string fileName = Guid.NewGuid().ToString("N") + file.FileName.Substring(file.FileName.LastIndexOf('.'));

            if (file != null)
            {
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }

                file.SaveAs(savePath + fileName);

                context.Response.Write(string.Format("{{success:1,msg:'文件上传成功！',data:{{filepath:'{0}'}}}}", localPath + fileName));
            }
            else
            {
                context.Response.Write("{success:0,msg:'文件上传失败！',data:''}");
            }
        }
        catch (Exception e)
        {
            context.Response.Write("{success:0,msg:'文件上传失败！',data:''}");
        }      
    }

    //*******************************************
    //功能概要       ：保存裁剪后的图片
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：保存裁剪后的图片
    //作成者         ：姚志强
    //作成日         ：2010-03-25
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void SaveCutterImage(HttpContext context)
    {
        HttpPostedFile file = context.Request.Files["FileData"];

        string virtualPath = "~/upLoadPic";
        string rootPicUrl = System.Configuration.ConfigurationManager.AppSettings["Picture.RootUrl"];

        string strSavePath = context.Server.MapPath(virtualPath + "/originalImage" + "\\");

        string fileName = Guid.NewGuid().ToString("N") + ".jpg";
        
        if (file != null)
        {
            if (!Directory.Exists(strSavePath))
            {
                Directory.CreateDirectory(strSavePath);
            }

            file.SaveAs(strSavePath + fileName);

            context.Response.Write("upLoadPic/originalImage/" + fileName);
        }
        else
        {
            context.Response.Write("0");
        } 
    }

    //*******************************************
    //功能概要       ：保存压缩后的图片
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：保存压缩后的图片
    //作成者         ：姚志强
    //作成日         ：2010-03-25
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void SaveThumbnialImage(HttpContext context)
    {
        HttpPostedFile file = context.Request.Files["FileData"];

        string virtualPath = "~/upLoadPic";
        string rootPicUrl = System.Configuration.ConfigurationManager.AppSettings["Picture.RootUrl"];

        string strSavePath = context.Server.MapPath(virtualPath + "/originalImage" + "\\");
        
        string fileName = Guid.NewGuid().ToString("N") + ".jpg";
        
        if (file != null)
        {
            if (!Directory.Exists(strSavePath))
            {
                Directory.CreateDirectory(strSavePath);
            }

            file.SaveAs(strSavePath + fileName);

            if (!Directory.Exists(context.Server.MapPath(virtualPath + "/thumbnailImage" + "\\")))
            {
                Directory.CreateDirectory(context.Server.MapPath(virtualPath + "/thumbnailImage" + "\\"));
            }
            
            string originalPath = strSavePath + fileName;
            string thumbnailPath = context.Server.MapPath(virtualPath + "/thumbnailImage" + "\\") + fileName;
            Chenxin.Lib.Utility.PictureProcess.MakeThumbnail(originalPath, thumbnailPath, 194, 116, "W");

            context.Response.Write(rootPicUrl + "thumbnailImage/" + fileName);
        }
        else
        {
            context.Response.Write("0");
        }
    }

    //*******************************************
    //功能概要       ：保存水印后的图片
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：保存压缩后的图片
    //作成者         ：姚志强
    //作成日         ：2010-03-25
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void SaveWaterMarkImage(HttpContext context)
    {
        HttpPostedFile file = context.Request.Files["FileData"];

        string virtualPath = "~/upLoadPic";
        string rootPicUrl = System.Configuration.ConfigurationManager.AppSettings["Picture.RootUrl"];

        string strSavePath = context.Server.MapPath(virtualPath + "/originalImage" + "\\");

        string fileName = Guid.NewGuid().ToString("N") + ".jpg";
        
        if (file != null)
        {
            if (!Directory.Exists(strSavePath))
            {
                Directory.CreateDirectory(strSavePath);
            }

            file.SaveAs(strSavePath + fileName);

            if (!Directory.Exists(context.Server.MapPath(virtualPath + "/waterMarkPicImage" + "\\")))
            {
                Directory.CreateDirectory(context.Server.MapPath(virtualPath + "/waterMarkPicImage" + "\\"));
            }
            string originalPath = strSavePath + fileName;
            string path_syp = context.Server.MapPath(virtualPath + "/waterMarkPicImage" + "\\") + fileName;
            string path_sypf = context.Server.MapPath(virtualPath + "/waterMarkBgImage" + "\\") + "logo.png";
            Chenxin.Lib.Utility.PictureProcess.AddWaterPic(originalPath, path_syp, path_sypf);
            
            //context.Response.Write("1");
            context.Response.Write(rootPicUrl + "waterMarkPicImage/" + fileName);
        }
        else
        {
            context.Response.Write("0");
        }
    }
}
